package ejb.userManagement;


import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import ejb.userManagement.SessionMgr;
import entities.User;

@Stateful
public class SessionBean implements SessionMgr {
	
	@Resource
	private EJBContext context;
	
	@PersistenceContext
    private EntityManager em;
	
	private User loggedUser;

	@Override
	public String getUserEmail () {
		checkUser();
		return loggedUser.getUserId();
	}
	
	@Override
	public String getUserFirstName () {
		checkUser();
		return loggedUser.getFirstName();
	}
	
	@Override
	public String getUserId () {
		return context.getCallerPrincipal().getName();
	}
	
	//Get the istance of the logged user
	private void checkUser() {
		if(loggedUser == null) {
			loggedUser = em.find(User.class, context.getCallerPrincipal().getName());
		}
	}

}
